package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.database.DataBase;
import com.projet.whattowatch.objet.Film;
import com.projet.whattowatch.objet.Humain;
import com.projet.whattowatch.objet.Utilisateur;
import com.projet.whattowatch.activity.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SignInActivity extends Activity
{
	private Button signInButton;
	private EditText nom;
	private EditText prenom;
	private EditText email;
	private EditText pw; // PassWord
	private EditText pwc; // PassWordConfirm
	private DataBase app_db;
	private EditText age;
	private Spinner sex;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sign_in_layout);

		app_db = new DataBase(getApplicationContext());

		nom = (EditText) findViewById(R.id.nomEditText);
		prenom = (EditText) findViewById(R.id.prenomEditText);
		email = (EditText) findViewById(R.id.emailEditText);
		pw = (EditText) findViewById(R.id.passwordEditText);
		pwc = (EditText) findViewById(R.id.confirmPasswordEditText);
		age = (EditText) findViewById(R.id.ageEditText);
		sex = (Spinner) findViewById(R.id.sexSpinner);
		
		signInButton = (Button) findViewById(R.id.signInButton);
		signInButton.setOnClickListener(signInListener);
	}

	public OnClickListener signInListener = new OnClickListener()
	{
		@Override
		public void onClick(View arg0)
		{
			String nomStr = nom.getText().toString();
			String prenomStr = prenom.getText().toString();
			String emailStr = email.getText().toString();
			String passwordStr = pw.getText().toString();
			String passwordcStr = pwc.getText().toString();
			String ageStr = age.getText().toString();
			String sexStr = sex.getSelectedItem().toString();
			
			if (isInteger(ageStr))
			{
				Utilisateur user = new Utilisateur(nomStr, prenomStr, emailStr, sexStr, passwordStr, new ArrayList<Humain>(),new ArrayList<Humain>(),new ArrayList<String>(),new ArrayList<Film>(),Integer.parseInt(ageStr));
			
				boolean notExist = true;
				for(int i = 0; i < MainActivity.mLibUtilisateur.getlistUtilisateur().size(); i++)
				{
					if(MainActivity.mLibUtilisateur.getlistUtilisateur().get(i).getAdresseMail().equals(emailStr))
						notExist = false;
				}
				
				if(notExist)
				{
					if(passwordStr.equals(passwordcStr))
					{
						MainActivity.mLibUtilisateur.addUtilisateur(user);
						app_db.addUser(user);
						finish();
					}
					else
					{
						Toast.makeText(
								getApplicationContext(),
								"Erreur : les mots de passe entrés ne correspondent pas.",
								Toast.LENGTH_SHORT).show();
					}
				}
				else
				{
					Toast.makeText(getApplicationContext(),
							"Cette adresse mail est déjà utilisée !",
							Toast.LENGTH_SHORT).show();
				}
			}
			else
			{
				Toast.makeText(
						getApplicationContext(),
						"Erreur : introduisez une valeur pour l'age.",
						Toast.LENGTH_SHORT).show();
			}
		}
	};
	public static boolean isInteger(String s) {
	    return isInteger(s,10);
	}

	public static boolean isInteger(String s, int radix) {
	    if(s.isEmpty()) return false;
	    for(int i = 0; i < s.length(); i++) {
	        if(i == 0 && s.charAt(i) == '-') {
	            if(s.length() == 1) return false;
	            else continue;
	        }
	        if(Character.digit(s.charAt(i),radix) < 0) return false;
	    }
	    return true;
	}
}